package com.xckj.cycling.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.xckj.cycling.utils.DensityUtil

class RoundProgressView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var color = Color.parseColor("#FA2C1E")
    private val paint = Paint()
    private val stroke = DensityUtil.dip2px(context, 10f)
    private var progress: Float = 0.75f

    init {
        paint.strokeWidth = stroke.toFloat()
        paint.style = Paint.Style.STROKE
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val radius = measuredWidth / 2f - stroke
        val arc = stroke.toFloat()
        canvas?.apply {
            paint.setColor(Color.parseColor("#33FA2C1E"))
            drawCircle(measuredWidth / 2f, measuredHeight / 2f, radius, paint)
            paint.strokeCap = Paint.Cap.ROUND
            paint.setColor(color)
            drawArc(
                arc,
                arc,
                measuredWidth.toFloat() - arc,
                measuredHeight.toFloat() - arc,
                -90f,
                progress * 360,
                false,
                paint
            )
        }
    }

    fun setProgress(fl: Float) {
        this.progress = fl
        invalidate()
    }

    fun setColor(parseColor: Int) {
        this.color = parseColor
        invalidate()
    }
}